package cn.tedu.jdbc;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Scanner;

/**
 * @Author Justin Parker
 * @Date 2024/7/30 0030 16:21
 * @Version 1.0
 */
/*
   登录功能
   1. 输入用户名
   2. 输入密码
 */
public class JDBCLogin01 {
    public static void main(String[] args) {
        try {
            Connection connection = DBUtil.getConnection();

            Scanner scanner = new Scanner(System.in);
            System.out.println("请输入用户名：");
            String username = scanner.nextLine();
            System.out.println("请输入密码：");
            String password = scanner.nextLine();
            Statement statement = connection.createStatement();
            String sql = "select nickname from userinfo where " +
                    "username='"+username+"' " +
                    "and password ='"+password+"'" ;
            ResultSet resultSet = statement.executeQuery(sql);
            //判断
            if(resultSet.next()){
                System.out.println("登录成功！");
                System.out.println("欢迎："+resultSet.getString("nickname"));
            }else {
                System.out.println("用户名或密码错误！");
            }

        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }
}
